-- tacticsM
-- Create by panyl
-- 策略模块
-- 目前仅支持一些简单的迷宫操作: 开始迷宫-离开迷宫

module("tacticsM", package.seeall);

-- local checkTactics;

local toDoTactics = {};

function init()
    -- 定时执行策略
    --cc.Director:getInstance():getScheduler():scheduleScriptFunc(checkTactics, 1, false);

    -- 注册消息回调
    register_msg_triggers();
end

function update()
    -- 遍历之前先把所有玩家取出来
    local users = table.keys(toDoTactics);
    for _, account in pairs(users) do
        local m = toDoTactics[account];
        if m then
            local user    = m["user"];
            local tactics = m["tactics"] or {};

            -- 遍历所有的策略
            for _, t in pairs(table.keys(tactics)) do
                tryDoTactics(user, t, tactics[t]);
            end
        end
    end
end

-- 执行策略
function tryDoTactics(user, tactics, data)
    if not data then
        return;
    end

    if tactics == "prepare_start" then
        local startTime = data["start_time"] or 0;
        local t = math.random(6, 30);

        if startTime + t <= os.time() then
            stop(user, tactics);
            start(user, "start_dungeon");
        end
    elseif tactics == "start_dungeon" then
        -- 开始迷宫
        -- 直接发包做策略
        local startTime = data["start_time"] or 0;

        if startTime <= os.time() then
            doTactics(user, tactics);
        end
    elseif tactics == "next_dungeon" then
        -- 进入下一层
        local layer = data.layer or 1;
        local useTime = 30 * (math.modf(layer / 10) + 1);

        if os.time() < data.start_time + useTime then
            return;
        end

        -- 开始进入下一层
        doTactics(user, tactics);

        -- 重新开启一个策略
        start(user, tactics);
    elseif tactics == "leave_dungeon" then
        -- 这里不需要通知服务器，但是需要定时其启动下次进入迷宫的策略
        stop(user, tactics);

        -- 随机一个时间(1~5分钟)
        local startTime = os.time() + math.random(60, 300);
        start(user, "start_dungeon", {["start_time"] = startTime, });
    end
end

-- 策略
function doTactics(user, tactics, data)
    local v = {
        ["tactics"] = tactics,
        ["data"] = data or {},
    };
    user:sendCmd("CMD_STRESS_TEST_TACTICS", v);
end

-- 开始策略
function start(user, tactics, info)
    local m = toDoTactics[user.userName] or {};

    m["user"] = user;

    local t = m["tactics"] or {};

    -- 记录策略开始时间
    t[tactics] = info or {};
    t[tactics]["start_time"] = os.time();

    m["tactics"] = t;
    toDoTactics[user.userName] = m;
end

function stop(user, tactics)
    local m = toDoTactics[user.userName];
    if not m then
        return;
    end

    local t = m["tactics"];
    if not t or not t[tactics] then
        return;
    end

    -- 直接清掉
    t[tactics] = nil;
end

function register_msg_triggers()
    CommandM.registerMsgTrigger("stress_tactics", "MSG_STRESS_TEST_TACTICS", function(user, args)
        if args.tactics == "start_dungeon" then
            -- 开始迷宫
            user.dungeonInfo = { ["layer"] = 1, };
            -- 下一层
            stop(user, "start_dungeon");
            start(user, "next_dungeon");
        elseif args.tactics == "leave_dungeon" then
            stop(user, "next_dungeon");
            start(user, "leave_dungeon");
        elseif args.tactics == "next_dungeon" then
            user.dungeonInfo["layer"] = args.data.layer;
        end
    end);

    -- 完全登录成功了
    CommandM.registerMsgCallback("stress_tactics", "MSG_LOGIN_NOTIFY_OK", function(user, args)
        start(user, "prepare_start");
    end);
end
